<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\response\Json;
use think\Request;
use app\admin\model\admin\User as UserModel;
/**
 * 权限
 * Class Auth
 * @package app\admin\controller
 */
class Auth extends Controller
{
    /**
     * 后台用户登录
     * @return Json
     */
    public function login(): Json
    {
        $model = new UserModel;
        if (($userInfo = $model->login($this->postData())) === false) {
            return $this->renderError($model->getError() ?: '登录失败');
        }
        return $this->renderSuccess([
            'accessToken' => $model->getToken()
        ], '登录成功');
    }

    /**
     * 用户详情
     * @return Json
     */
    public function info(): Json
    {
        $userInfo = UserModel::detail($this->admin['admin_user_id'])->toArray();
        $model = new UserModel;
        $userInfo['desc'] = '';
        $userInfo['userId'] = $userInfo['admin_user_id'];
        $userInfo['realName'] = $userInfo['nick_name'];
        $userInfo['roles'] = [];
        $userInfo['homePath'] = '/';
        $userInfo['token'] = $model->getToken();
        $userInfo['role_name'] = $userInfo['role_id']==10001?'Supper':'';
        return $this->renderSuccess($userInfo, '登录成功');
    }

    /**
     * 刷新token
     * @return String|Json
     */
    public function refresh(): String|Json
    {
        $model = new UserModel;
        $token = $model->refreshToken();
        if ($token != false) {
            return $token;
        }else {
            return $this->renderError('token刷新失败');
        }
    }
}
